package com.lwgk.utils;

import android.content.Intent;
import android.os.SystemClock;
import android.view.View;

/**
 * created by 赵航
 * 联系微信 微信号：zhao--hang--
 * on 2019/7/11
 * --------------------------------------------------------------------------
 * 描述：点击按钮,防止多次跳转的工具类
 * 注意事项：注意使用方法
 */
public class ClickUtils {

    // 两次点击按钮之间的点击间隔不能少于1000毫秒
    private static final int MIN_CLICK_DELAY_TIME = 500;
    private static long lastClickTime;

    /**
     * @return   btn.setOnClickListener(new View.OnClickListener() {
     *             @Override
     *             public void onClick(View v) {
     *                 if (Utils.isFastClick()) {
     *                     // 进行点击事件后的逻辑操作
     *                 }
     *             }
     *         });
     */
    public static boolean isFastClick() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }
    private String mActivityJumpTag;        //activity跳转tag
    private long mClickTime;                //activity跳转时间

    /**
     * @param intent 检查是否重复跳转，不需要则重写方法并返回true
     * @return public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
     *               if (checkDoubleClick(intent)) {
     *                   super.startActivityForResult(intent, requestCode, options);
     *                    进行回调事件操作，禁止一些反复强制用户的流氓行为
     *               }
     */
    protected boolean checkDoubleClick(Intent intent) {

        // 默认检查通过
        boolean result = true;
        // 标记对象
        String tag;
        if (intent.getComponent() != null) { // 显式跳转
            tag = intent.getComponent().getClassName();
        }else if (intent.getAction() != null) { // 隐式跳转
            tag = intent.getAction();
        }else {
            return true;
        }

        if (tag.equals(mActivityJumpTag) && mClickTime >= SystemClock.uptimeMillis() - MIN_CLICK_DELAY_TIME) {
            // 检查不通过
            result = false;
        }

        // 记录启动标记和时间
        mActivityJumpTag = tag;
        mClickTime = SystemClock.uptimeMillis();
        return result;
    }

}
